home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6098 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  50 lines

  1. Newsgroups: comp.lang.c
  2. Path: undergrad.math.uwaterloo.ca!clgonsal
  3. From: clgonsal@undergrad.math.uwaterloo.ca (Carl Laurence Gonsalves)
  4. Subject: Re: writing to .exe file
  5. Sender: news@undergrad.math.uwaterloo.ca (news spool owner)
  6. Message-ID: <Dn6pHy.DoI@undergrad.math.uwaterloo.ca>
  7. Date: Thu, 22 Feb 1996 15:53:58 GMT
  8. References: <4g961o$gdr@ultra.sonic.net> <4gbkak$d42@news-f.iadfw.net>
  9. Nntp-Posting-Host: cayley.uwaterloo.ca
  10. Organization: University of Waterloo
  11.  
  12. In article <4gbkak$d42@news-f.iadfw.net>, bob <alpet@airmail.net> wrote:
  13. >Ted Rollheiser <trollhei@sonic.net> wrote:
  14. >
  15. >>I would like  to save data ( a string ) from one run of a (dos) program 
  16. >>to the next by writing it to the .exe file. does this sound doable?
  17. >
  18. >Sure, programs do it all the time.  Important part is to be sure space
  19. >is allocated for your string, and that you have some marker to find
  20. >the string.  Do something like this:
  21. >
  22. >struct blah
  23. >{
  24. >   char   marker[10]="!@#$%%$#@!";
  25. >   char   comment[80];
  26. >}
  27. >
  28. >This allocats a structure in your .exe file.  When your program runs,
  29. >open the file (your .exe) in binary mode and scan for the marker.
  30. >When you find it, you know the next 80 bytes are yours to play with.
  31. >Modify to your hearts content.  A side note is a good idea to *not*
  32. >stop at the first occurence of your marker.  It might be a  fluke and
  33. >some real code in your program may dup this marker if the you find is
  34. >not yours, overwriting the next 80 bytes could be real bad.  So go
  35. >ahead and scan the whole file to be sure it doesn't occur again.
  36.  
  37. A simpler approach might be to write a separate program that appends your
  38. data to the end of the executable after it's been compiled (and linked).
  39. You can then safely assume that the x number of bytes on the end of the
  40. executable are your data.
  41.  
  42. fseek( fp, -sizeof(myData), SEEK_END );
  43. fread( &myData, sizeof(myData), 1, fp );
  44.  
  45. -- 
  46.         Carl Laurence Gonsalves - clgonsal@undergrad.math.uwaterloo.ca
  47.                    Computer Science, University of Waterloo
  48.                http://www.undergrad.math.uwaterloo.ca/~clgonsal/
  49.                    http://www.csclub.uwaterloo.ca/~clgonsal/
  50.